$(function() {
	title_block();
	content_block();
	secret_block();
	name_block();
	email_block();
	$("#release_confirm").click(release_confirm);
	
	page_block();
});

function title_block() {
	$("#ms_title_warning").hide();
	$("#ms_title").focus(title_remind);
	$("#ms_title").blur(check_title);
}

function title_remind() {
	$("#ms_title_warning").hide();
	$("#ms_title_reminding").show();
}

function title_warn(info) {
	$("#ms_title_reminding").hide();
	$("#ms_title_warning").text(info);
	$("#ms_title_warning").show();
}

function check_title() {
	var title = $("#ms_title").val();
	if (title == null || title == "") {
		title_warn("标题不能为空");
		return false;
	}
	if (title.length > 30) {
		title_warn("标题不能超过30个字符");
		return false;
	}
	return title;
}

function content_block() {
	$("#ms_content_warning").hide();
	$("#ms_content").focus(content_remind);
	$("#ms_content").blur(check_content);
}

function content_remind() {
	$("#ms_content_warning").hide();
	$("#ms_content_reminding").show();
}

function content_warn(info) {
	$("#ms_content_reminding").hide();
	$("#ms_content_warning").text(info);
	$("#ms_content_warning").show();
}

function check_content() {
	var content = $("#ms_content").val();
	if (content == null || content == "") {
		content_warn("内容不能为空");
		return false;
	}
	return content;
}

function secret_block() {
	$("#ms_secret").click(check_pwbox);
	$("#pw_box").hide();
	$("#ms_password_warning").hide();
	$("#ms_password").focus(password_remind);
	$("#ms_password").blur(check_password);
}

function password_remind() {
	$("#ms_password_warning").hide();
	$("#ms_password_reminding").show();
}

function password_warn(info) {
	$("#ms_password_reminding").hide();
	$("#ms_password_warning").text(info);
	$("#ms_password_warning").show();
}

function check_password() {
	var pw = $("#ms_password").val();
	if (pw == null || pw == "") {
		password_warn("密码不能为空");
		return false;
	}
	return pw;
}

function check_pwbox() {
	var checked = secret_checked();
	if (checked == true) {
		password_remind();
	}
	$("#pw_box").toggle(250);
}

function secret_checked() {
	var checked = document.getElementById("ms_secret").getAttribute("checked");
	return checked;
}

function name_block() {
	$("#ms_name").focus(name_remind);
	$("#ms_name").blur(check_name);
}

function name_remind() {
	$("#ms_name_warning").hide();
}

function name_warn(info) {
	$("#ms_name_warning").text(info);
	$("#ms_name_warning").show();
}

function check_name() {
	var name = $("#ms_name").val();
	if (name == null || name == "") {
		name_warn("姓名不能为空");
		return false;
	}
	return name;
}

function email_block() {
	$("#ms_email").focus(email_remind);
	$("#ms_email").blur(check_email);
}

function email_remind() {
	$("#ms_email_warning").hide();
}

function email_warn(info) {
	$("#ms_email_warning").text(info);
	$("#ms_email_warning").show();
}

function check_email() {
	var email = $("#ms_email").val();
	if (email == null || email == "") {
		email_warn("邮箱不能为空");
		return false;
	}
	return email;
}

function release_confirm() {
	var title = check_title();
	if (title == false) {
		return;
	}
	
	var content = check_content();
	if (content == false) {
		return;
	}
	
	var type = $("#ms_type").val();
	
	var secret = secret_checked();
	var password = "";
	if (secret == true) {
		secret = "true";
		password = check_password();
		if (password == false) {
			return;
		}
	} else {
		secret = "false";
	}
	
	$.post('release.jsp', 'title='+title+'&content='+content+ '&type='+ type+ '&secret='+ secret+ '&password='+ password, function(data) {
		json = eval('('+data+')');
		var no = json.no;
		
		if (no == null || no == "") {
			show_fail();
		} else {
			show_succeed(no);
		}
	});
}

function page_block() {
	$(".nm_succeed").hide();
	$(".nm_fail").hide();
}

function show_succeed(no) {
	$(".nm_body").hide();
	$(".nm_fail").hide();
	$(".nm_succeed").show();
	$("#result_area").text(no);
}

function show_fail() {
	$(".nm_body").hide();
	$(".nm_succeed").hide();
	$(".nm_fail").show();
}